package com.eece496.Server;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eece496.Database.Enrollment;
import com.eece496.Helpers.CourseInfo;

/**
 * Servlet implementation class StudentBackPortalServlet
 */
public class StudentBackPortalServlet extends MasterServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentBackPortalServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// Connect to the Database
		Connect();
		
		
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String sID = request.getParameter("studentID");
		Integer studentID = Integer.parseInt(sID);
		
		//Variable for determining if the student has an empty course list
		String isEmpty = "false";
		ArrayList<CourseInfo> info = new ArrayList<CourseInfo>();
		
		Enrollment myEnrolledCourses = new Enrollment();
		myEnrolledCourses.Setup(connection);
		
		//Get student information
		try {
			info = myEnrolledCourses.GetStudentCourseInfo(studentID);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		//check if the courselist is empty
		if(info.size() == 0)
		{
			isEmpty.equals("true");
			System.out.println(isEmpty);
		}
		
		//Forward data to jsp
		request.setAttribute("firstname", firstname);		
		request.setAttribute("lastname", lastname);	
		request.setAttribute("CourseInfo", info);		
		request.setAttribute("isEmpty", isEmpty);	
		request.setAttribute("studentID", sID);
		
		request.getRequestDispatcher("/student/student-portal.jsp").forward(
				request, response);	
	
	}

}
